#include <gtest/gtest.h>
#include <fstream>
#include "world/WorldFactory.h"
#include "cell/GenericCellStateChars.h"
#include "world/GenericWorldSize.h"
#include "world/World.h"

using namespace std;

enum {MAX_ROW = 20, MAX_COL = 20, MAX_NUM = MAX_ROW * MAX_COL};


namespace
{
    char init[MAX_NUM];

    void fillInit(const string str)
    {
        ifstream ifstrm(str);
        if(!ifstrm.good())
        {
            cout<<"read fail"<<endl;
            return;
        }
        for(int i = 0; i < MAX_NUM; i++)
        {
            char tmp;
            ifstrm>>tmp;
            if(tmp == '\n') ifstrm>>tmp;
            init[i] = tmp;
        }
        ifstrm.close();
    }

    void output(const string& str, World& world)
    {
        ofstream of(str);
        world.output(of, output_chars);
        world.output(cout, output_chars);
        of.close();
    }
}

int main(int argc, char** argv)
{
    fillInit("init.txt");

    World& world = WorldFactory::create();

    world.startWith(init, input_chars, GenericWorldSize<20,20>());
    for(int i = 0; i< 55; i++)
    {
        world.nextGeneration();
    }

    output("final.txt", world);

    testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}
